package com.chinatgl.latte.net.rx;

import android.content.Context;

import com.chinatgl.latte.net.HttpMethod;
import com.chinatgl.latte.net.RestClientBuilder;
import com.chinatgl.latte.net.RestCreator;
import com.chinatgl.latte.net.RestService;
import com.chinatgl.latte.net.callback.IError;
import com.chinatgl.latte.net.callback.IFailure;
import com.chinatgl.latte.net.callback.IRequest;
import com.chinatgl.latte.net.callback.ISuccess;
import com.chinatgl.latte.net.callback.RequsetCallbacks;
import com.chinatgl.latte.net.download.DownloadHandler;
import com.chinatgl.latte.ui.LatteLoader;
import com.chinatgl.latte.ui.LoaderStyle;

import java.io.File;
import java.util.WeakHashMap;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;

/**
 * 文件描述: 网络请求
 * 作者: Created by 杨伟乔 on 2017/8/7 0007.
 * 版本号: 1.0
 */

public class RxRestClient {


    private final String URL;
    private static final WeakHashMap<String, Object> PARAMS = RestCreator.getParams();
    private final RequestBody BODY;
    private final LoaderStyle LOADER_STYLE;
    private final Context CONTEXT;
    private final File FILE;
    public RxRestClient(String url,
                        WeakHashMap<String, Object> params,
                        RequestBody body,
                        File file,
                        LoaderStyle loaderStyle,
                        Context context
    ) {

        this.URL = url;
        PARAMS.putAll(params);
        this.BODY = body;
        this.FILE = file;
        this.LOADER_STYLE = loaderStyle;
        this.CONTEXT = context;
    }

    public static RxRestClientBuilder builder() {

        return new RxRestClientBuilder();
    }


    private Observable<String> request(HttpMethod httpMethod) {

        final RxRestService service = RestCreator.getRxRestService();
//        Call<String> call = null;
        Observable<String> observable = null;
        //开始请求之前 判断回调接口是否传入


        if (LOADER_STYLE != null) {
            LatteLoader.showLoading(CONTEXT, LOADER_STYLE);
        }

        switch (httpMethod) {

            case GET:
                observable = service.get(URL, PARAMS);
                break;

            case POST:
                observable = service.post(URL, PARAMS);
                break;
            case POST_RAW:
                observable = service.postRaw(URL, BODY);
                break;
            case PUT:
                observable = service.put(URL, PARAMS);
                break;

            case PUT_RAW:
                observable = service.postRaw(URL, BODY);
                break;

            case DELETE:
                observable = service.delete(URL, PARAMS);
                break;

            case UPLOAD:
                final  RequestBody requestBody = RequestBody.create(MediaType.parse(MultipartBody.FORM.toString()),FILE);
                final MultipartBody.Part body =
                        MultipartBody.Part.createFormData("file", FILE.getName(), requestBody);
                observable = service.upLoad(URL, body);
                break;
            case DOWNLOAD:


                break;
            default:
                break;
        }

      return observable;
    }




    public final Observable<String> get() {
        return   request(HttpMethod.GET);
    }

    public final Observable<String> post() {
        if (BODY == null) {
            return    request(HttpMethod.POST);

        } else {
            if (!PARAMS.isEmpty()) {
                throw new RuntimeException("postRaw请求参数必须为空");
            }
            return    request(HttpMethod.POST_RAW);

        }
    }

    public final Observable<String> delete() {
     return    request(HttpMethod.DELETE);
    }

    public final Observable<String> put() {
        if (BODY == null) {
            return   request(HttpMethod.PUT);
        } else {
            if (!PARAMS.isEmpty()) {
                throw new RuntimeException("PUT_RAW请求参数必须为空");
            }
            return   request(HttpMethod.PUT_RAW);
        }
    }

    /**
     * 下载方法
     */
    public final Observable<ResponseBody> download() {
        final Observable<ResponseBody>  responseBodyObservable = RestCreator.getRxRestService().downLoad(URL,PARAMS);

//         new DownloadHandler(URL, REQUEST, DOWNLOAD_DIR, EXTENSION, NAME,
//                SUCCESS, FAILURE, ERROR)
//                .handleDownload();
        return responseBodyObservable;
    }
}
